home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- /* function prototype */
- int getvalue(char *s, char** name, char** version);
-
- main()
- {
- char *gateway_interface;
- char *name, *version;
-
- /* output html MIME type */
- printf("Content-type: text/html\n\n");
-
- printf("<HTML>\n");
- printf("<HEAD><TITLE>CGI Script How-to: Test Script</TITLE></HEAD>\n");
- printf("<BODY>\n");
-
- printf("<H1>CGI Script How-To determine the version of CGI being used</H1>\n");
-
- gateway_interface = getenv("GATEWAY_INTERFACE");
-
- /* If name/version strings have been extracted then getvalue returns 0
- * otherwise the value is NULL or in the wrong format.
- */
-
- if (getvalue(gateway_interface, &name, &version) == 0)
- {
- /* Use the gateway interface value here */
- printf("Gateway Interface: name = <B>%s</B> version = <B>%s</B>\n", name, version);
- }
- else
- {
- printf("Gateway interface is undefined or invalid!\n");
- }
-
- printf("</BODY></HTML>\n\n");
- exit(0);
- }
-
-
- /*
- * function getvalue()
- *
- * Parses an input string (s) in the form name/version, extracts both the name
- * and version elements and stores these in two output string variables (name,
- * version).
- *
- * Returns: 0 if name/version values extracted from target string,
- * -1 if values not present
- */
-
- int getvalue(char *s, char** name, char** version)
- {
- char *p;
- if (s == NULL || *s == '/')
- {
- return -1; /* null string or no name field */
- }
-
- p = strchr(s, '/'); /* Locate the slash (/) in the string */
- if (p == 0)
- {
- return -1; /* '/' character not found */
- }
-
- *name = malloc(p-s+1);
- if (*name == NULL)
- {
- return -1; /* malloc failed */
- }
- strncpy(*name, s, p-s);
- (*name)[p-s] = '\0'; /* terminate the string */
-
- *version = malloc(strlen(p));
- if (*version == NULL)
- {
- return -1; /* malloc failed */
- }
- strcpy(*version, p+1);
-
- return 0; /* okay, value set */
- }
-
- /*
- * end of testgway.c
- */
-